import numpy as np


class Mortgage(object):
    """
    房贷计算器
    等额本金
    等额本息
    """
    def __init__(self, loan_amount, year_rate, loan_year):
        """
        :param loan_amount:
        :param year_rate:
        :param loan_year:
        """
        self.loan_amount = loan_amount
        self.month_rate = year_rate / 12
        self.loan_month = loan_year * 12

    def principal_interest(self):
        """
        等额本息
        每月还款额=贷款本金×[月利率×(1+月利率) ^ 还款月数]÷{[(1+月利率) ^ 还款月数]-1}
        :param loan_amount: 贷款额
        :param rate: 月利率
        :param months: 还款月数
        :return:
        """
        molecular = self.month_rate * np.power(1+ self.month_rate, self.loan_month)
        denominator = np.power(1 + self.month_rate, self.loan_month) - 1
        repayments = 10000 * self.loan_amount * molecular / denominator

        total_interest = self.loan_month * repayments / 10000 - self.loan_amount
        return round(repayments, 2), round(total_interest, 4)

    def principal(self, month):
        """
        等额本金
        每月还款额=贷款本金÷贷款期月数+（本金-已归还本金累计额）×月利率
        :param loan_amount: 贷款额
        :param months_rate: 月利率
        :param months: 还款月数
        :param month: 还款月
        :return:
        """
        def get_total_interest(pricipal):
            """
            总利息
            :param pricipal:
            :return:
            """
            return pricipal * self.month_rate * (1 + self.loan_month) * self.loan_month / 2

        pricipal = self.loan_amount / self.loan_month
        interest = self.month_rate * (self.loan_amount - pricipal * (month - 1))
        repayments = 10000 * (pricipal + interest)

        total_interest = get_total_interest(pricipal)
        return round(repayments, 2), round(total_interest, 4)


def run(loan_amount, year_rate, loan_year):
    """
    主程序，入口程序
    :param loan_amount: 贷款总额（万元）
    :param year_rate: 年利率
    :param loan_year: 贷款年限
    :return:
    """
    mortgage = Mortgage(loan_amount=loan_amount, year_rate=year_rate, loan_year=loan_year)

    repayments_principal_interest, total_principal_interest = mortgage.principal_interest()
    repayments_principal_1, total_principal_1 = mortgage.principal(month=1)
    repayments_principal_2, total_principal_2 = mortgage.principal(month=2)

    months = (repayments_principal_1 - repayments_principal_interest) / (
                repayments_principal_1 - repayments_principal_2)

    info = '贷款额：{}万\n年利率：{}%\n贷款年限：{}年\n\n\n'.format(loan_amount, year_rate * 100, loan_year)
    info += '等额本息总利息： {}万元\n每月还款：{}元\n\n'. \
        format(total_principal_interest, repayments_principal_interest)
    info += '等额本金总利息： {}万元\n第一个月还款：{}元\n等额本金每月还款额递减：{}元\n\n'. \
        format(total_principal_1, repayments_principal_1, round(repayments_principal_1 - repayments_principal_2))
    info += '等额本金总的月还款额不高于等额本息月还款额，大致需要：{}年'.format(months / 12)

    print(info)


if __name__ == '__main__':
    loan_amount = 160
    year_rate = 0.049
    loan_year = 20

    run(loan_amount, year_rate, loan_year)
